﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Partial Class FrontEnd_Control_Menu_LeftMenuTableMenu
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            If Not Page.RouteData.Values("MenuId") Is Nothing Then
                hidRootMenuId.Value = Page.RouteData.Values("MenuId")
            End If
            hidNavigateUrl.Value = Request.Url.ToString
            BindMenu()
        End If
    End Sub
    Protected Sub BindMenu()
        Using Data As New VSEntities
            'Add DomainName DUC 09-09-2014
            Dim p = (From q In Data.Menus Where q.Domain = DomainName And q.ParentId = 0 Select q Order By q.Sort).ToList
            With (rptMenu)
                .DataSource = p
                .DataBind()
            End With
        End Using
    End Sub
    'lay nhanh goc cua category theo catid truyen vao
    Protected Function getRootMenu(ByVal _MenuId As Integer) As Integer
        Dim _hientaiMenuId = _MenuId
        Using Data As New VSEntities
            'Add DomainName DUC 09-09-2014
            Dim p = (From q In Data.Menus Where q.Domain = DomainName And q.MenuId = _hientaiMenuId Select q).FirstOrDefault
            If p Is Nothing Or p.ParentId = 0 Then
                Return _hientaiMenuId
            Else
                Return getRootMenu(p.ParentId)
            End If
        End Using
        Return _hientaiMenuId
    End Function

    Protected Sub rptMenu_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound
        Dim row As VSModel.Menu = CType(e.Item.DataItem, VSModel.Menu)
        'Dim hplTitle As HyperLink = e.Item.FindControl("hplTitle")
        Dim hplTitle As HyperLink = e.Item.FindControl("hplTitle")
        hplTitle.Text = row.Title
        If Not row.NavigateUrl Is Nothing Then
            hplTitle.NavigateUrl = MainDomain & row.NavigateUrl
        Else
            hplTitle.NavigateUrl = MainDomain & "Content/" & row.QueryString & "-" & row.MenuId & ".html"
        End If

        If hidRootMenuId.Value.ToString.Contains(row.MenuId) Or (Not row.NavigateUrl Is Nothing AndAlso hidNavigateUrl.Value.Contains(row.NavigateUrl)) Then
            hplTitle.CssClass = "active"
        End If
    End Sub
End Class
